package com.code;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public abstract class ByteUtil {
	private static final Queue<Integer> queue = new LinkedList<Integer>();

	private static final short[] CRCHiByte = { 0x00, 0xC1, 0x81, 0x40, 0x01,
			0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
			0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
			0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
			0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
			0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
			0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
			0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
			0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
			0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
			0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80,
			0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
			0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
			0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
			0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
			0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
			0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
			0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
			0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80,
			0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
			0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
			0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
			0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80,
			0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 };

	private static final short[] CRCLoByte = { 0x00, 0xC0, 0xC1, 0x01, 0xC3,
			0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04,
			0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB,
			0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB,
			0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14,
			0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
			0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2,
			0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC,
			0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39,
			0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA,
			0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25,
			0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21,
			0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66,
			0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D,
			0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8,
			0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
			0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77,
			0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
			0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57,
			0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F,
			0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
			0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F,
			0x8D, 0x4D, 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46,
			0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 };

	public static String bytesToString(short[] dp) {
		if (dp == null)	return null;
		int plen = dp.length;
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < plen; i++) {
			str.append(String.format("%02X", dp[i]));
		}
		return str.toString();
	}
	public static String integerListToString(List<Integer> dp) {
		if (dp == null)	return null;
		int plen = dp.size();
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < plen; i++) {
			str.append(String.format("%02X", dp.get(i)));
		}
		return str.toString();
	}

	public static String integerListToString(Integer[] dp) {
		if (dp == null)	return null;
		int plen = dp.length;
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < plen; i++) {
			str.append(String.format("%02X", dp[i]));
		}
		return str.toString();
	}
	public static String bytesToString(List<Integer> dp){
        if(dp==null) return null;
        int plen=dp.size();
        StringBuffer str=new StringBuffer();
        for(int i=0;i<plen;i++){ 
            str.append(String.format("%02X",dp.get(i)));
        }
        return str.toString();
    }
	public static String bytesToString(byte[] dp) {
		if (dp == null)
			return null;
		int plen = dp.length;
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < plen; i++) {
			str.append(String.format("%02X", dp[i]));
		}
		return str.toString();
	}
	public static String bytesToAscString(byte[] dp) throws UnsupportedEncodingException {
		if (dp == null) 	return null;
		return new String(dp, "ISO8859-1");
	}
	public static String bytesToString(byte[] dp, int len) {
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < len; i++) {
			str.append(String.format("%02X", dp[i]));
		}
		return str.toString();
	}
	public static String bytesToStringNotStartWithFFAnd5A(byte[] dp, int len) {
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < len; i++) {
			if (str.length()==0){
				short v=byteToShort(dp[i]);
				if (255==v||0x5A==v) continue;
			}
			str.append(String.format("%02X", dp[i]));
		}
		return str.toString();
	}
	public static String bytesToBytesNotStartWithFFAnd5A(byte[] dp, int len) {
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < len; i++) {
			if (str.length()==0){
				short v=byteToShort(dp[i]);
				if (255==v||0x5A==v) continue;
			}
			str.append(String.format("%02X", dp[i]));
		}
		return str.toString();
	}
	public static String bytesToString2(byte[] dp, int len) {

		StringBuffer str = new StringBuffer();
		for (int i = 0; i < len; i++) {
			str.append((char) Integer.parseInt(String.format("%02X",  dp[i]), 16));
		}
		return str.toString();
	}
	/*
	 * public static byte[] hexStringToBytes(String hexString){ int
	 * count=hexString.length()/2; System.out.println("hexStringToBytes "+count
	 * +"  "+hexString); byte[] data=new byte[count-1]; try{ for (int
	 * i=0;i<count;i++){
	 * //System.out.println("hexStringToBytes : "+hexString.substring(i*2,
	 * i*2+2)); data[i]=Byte.valueOf(hexString.substring(i*2, i*2+2), 16);
	 * 
	 * } }catch(Exception e){ e.printStackTrace(); } return data; }
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}
	public static List<Integer> hexStringToIntegers(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		List<Integer> d = new ArrayList<Integer>();
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d.add(0x00FF &((byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]))));
		}
		return d;
	}
	public static String bytesToDateString(byte[] dp) {
		if (dp == null)
			return null;
		int plen = dp.length;
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < plen; i++) {
			str.append(String.format("%02d", dp[i]));
		}
		return str.toString();
	}

	public static String dateToHexDateString(Date date) {
		byte[] byteValue = new byte[6];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		byteValue[0] = (byte) (cal.get(Calendar.YEAR) - 2000);
		byteValue[1] = (byte) (cal.get(Calendar.MONTH) + 1);
		byteValue[2] = (byte) (cal.get(Calendar.DAY_OF_MONTH));
		byteValue[3] = (byte) (cal.get(Calendar.HOUR_OF_DAY));
		byteValue[4] = (byte) (cal.get(Calendar.MINUTE));
		byteValue[5] = (byte) (cal.get(Calendar.SECOND));
		return bytesToString(byteValue);
	}

	public static byte[] dateToHexDateByte(Date date) {
		byte[] byteValue = new byte[6];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		byteValue[0] = (byte) (cal.get(Calendar.YEAR) - 2000);
		byteValue[1] = (byte) (cal.get(Calendar.MONTH) + 1);
		byteValue[2] = (byte) (cal.get(Calendar.DAY_OF_MONTH));
		byteValue[3] = (byte) (cal.get(Calendar.HOUR_OF_DAY));
		byteValue[4] = (byte) (cal.get(Calendar.MINUTE));
		byteValue[5] = (byte) (cal.get(Calendar.SECOND));
		return byteValue;
	}

	/**
	 * 
	 * 时间转BCD码
	 * 
	 * @return
	 */
	public static String dateToBcdString(Date date) {
		byte[] byteValue = new byte[6];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		byteValue[0] = (byte) (cal.get(Calendar.YEAR) - 2000);
		byteValue[1] = (byte) (cal.get(Calendar.MONTH) + 1);
		byteValue[2] = (byte) (cal.get(Calendar.DAY_OF_MONTH));
		byteValue[3] = (byte) (cal.get(Calendar.HOUR_OF_DAY));
		byteValue[4] = (byte) (cal.get(Calendar.MINUTE));
		byteValue[5] = (byte) (cal.get(Calendar.SECOND));
		// return bytesToString(byteValue);
		return bytesToDateString(byteValue);
	}

	public static byte[] bytesToDateByte(byte[] dp) {
		if (dp == null)
			return null;
		int plen = dp.length;
		byte[] value = new byte[plen];
		for (int i = 0; i < plen; i++) {
			value[i] = Byte.valueOf(String.format("%02d", dp[i]), 16);
		}
		return value;
	}

	public static short[] bytesToDateBytes(byte[] dp) {
		if (dp == null)
			return null;
		int plen = dp.length;
		short[] value = new short[plen];
		for (int i = 0; i < plen; i++) {
			value[i] = dp[i];
			value[i] = Short.valueOf(String.format("%02X%02X", 0x00, dp[i]), 16);
		}
		return value;
	}

	public static int bytesToInt(byte rb) {
		return Integer.parseInt(String.format("%02X", rb), 16);
	}

	public static int bytesToInt(byte a, byte b) {
		return Integer.parseInt(String.format("%02X%02X", a, b), 16);
	}

	public static int bytesToInt(byte a, byte b, byte c, byte d) {
		return Integer.parseInt(String.format("%02X%02X%02X%02X", a, b, c, d),16);
	}

	public static char byteToChar(byte rb) {
		return (char) Integer.parseInt(String.format("%02X", rb), 16);
	}

	public static short byteToShort(byte rb) {
		return Short.parseShort(String.format("%02X", rb), 16);
	}

	public static float bytesToFloat(byte a, byte b, byte c, byte d) {
		try {
			float f = Float.intBitsToFloat((int) Long.parseLong(String.format("%02X%02X%02X%02X", a, b, c, d), 16));
			if (Float.isInfinite(f)) {
				return -9999;
			} else {
				return f;
			}

		} catch (Exception e) {
			// System.out.println(e.getMessage());
			return -9999;
		}
	}

	public static boolean crcValidate(byte[] bytes) {
		int j = bytes.length - 2;
		short[] shorts = new short[j];
		for (int i = 0; i < j; i++) {
			shorts[i] = byteToShort(bytes[i]);
		}
		return buildCrc16(shorts) == Integer.parseInt(String.format("%02X%02X", bytes[j], bytes[j + 1]), 16);
	}

	public static int buildCrc16(byte[] args) {
		int j = args.length - 2;
		short[] shorts = new short[j];
		for (int i = 0; i < j; i++) {
			shorts[i] = byteToShort(args[i]);
		}
		return buildCrc16(shorts);
	}

	public static int buildCrc16(int[] args) {
		int j = args.length - 2;
		short[] shorts = new short[j];
		for (int i = 0; i < j; i++) {
			shorts[i] = (short) args[i];
		}
		return buildCrc16(shorts);
	}
	public static int[] buildCrc16With(short[] args,int length,int[] crc16s) {
		crc16s[1] = 0xFF; // 高 CRC 字节初始化
		crc16s[2] = 0xFF; // 低 CRC 字节初始化
		short index; // CRC 循环中的索引
		for (int i = 0; i < length; i++){ // 传输消息缓冲区
		
			index = (short) (crc16s[1] ^ args[i]); // 计算CRC
			crc16s[1] = (short) (crc16s[2] ^ CRCHiByte[index]);
			crc16s[2] = CRCLoByte[index];
		}
		crc16s[0]=crc16s[1] << 8 | crc16s[2];
		return crc16s;
	}
	public static int[] buildCrc16At(short[] args,int index,int[] crc16s) {
		
		index = (short) (crc16s[1] ^ args[index]); // 计算CRC
		crc16s[1] = (short) (crc16s[2] ^ CRCHiByte[index]);
		crc16s[2] = CRCLoByte[index];
		crc16s[0] = crc16s[1]<< 8| crc16s[2];  
		return crc16s;
	}
	public static int buildCrc16(short[] args) {
		short crcHi = 0xFF; // 高 CRC 字节初始化
		short crcLo = 0xFF; // 低 CRC 字节初始化
		short index; // CRC 循环中的索引
		for (int i = 0; i < args.length; i++){ // 传输消息缓冲区
		
			index = (short) (crcHi ^ args[i]); // 计算CRC
			crcHi = (short) (crcLo ^ CRCHiByte[index]);
			crcLo = CRCLoByte[index];
		}
		int crc16 = crcHi << 8 | crcLo;
		return crc16;
	}
	public static int buildCrc16(short[] args,int length) {
		short crcHi = 0xFF; // 高 CRC 字节初始化
		short crcLo = 0xFF; // 低 CRC 字节初始化
		short index; // CRC 循环中的索引
		for (int i = 0; i < length; i++){ // 传输消息缓冲区
		
			index = (short) (crcHi ^ args[i]); // 计算CRC
			crcHi = (short) (crcLo ^ CRCHiByte[index]);
			crcLo = CRCLoByte[index];
		}
		int crc16 = crcHi << 8 | crcLo;
		return crc16;
	}
	public static int buildCrc16(String args) {
		int j = args.length() / 2;
		short[] shorts = new short[j];
		for (int i = 0; i < j; i++) {
			shorts[i] = Short.parseShort(args.substring(i * 2, (i + 1) * 2), 16);
		}
		return buildCrc16(shorts);
	}

	public static int next() {
		if (queue.isEmpty()){
			for (int i = 0; i < 256; i++) {
				queue.add(i);
			}
		}
		int next = queue.remove();
		queue.add(next);
		return next;
	}

	public static boolean sumCheck(byte[] bytes) {
		int length = bytes.length - 1;
		return byteSum(Arrays.copyOfRange(bytes, 0, length)) == bytes[length];
	}

	public static byte byteSum(String args) {
		int j = args.length() / 2;
		byte[] bytes = new byte[j];
		for (int i = 0; i < j; i++) {
			bytes[i] = (byte) (Short.parseShort(args.substring(i * 2, (i + 1) * 2), 16) & 255);
		}
		return byteSum(bytes);
	}

	public static byte byteSum(byte[] bytes) {
		int length = bytes.length;
		short c1 = 0;
		for (int i = 2; i < length; i++) {
			c1 += (short) bytes[i];
			c1 = (short) (c1 & 255);
		}
		return (byte) c1;
	}
	public static boolean sumCheck(short[] bytes){
    	int length=bytes.length-1;
    	return byteSum(Arrays.copyOfRange(bytes,0,length))==bytes[length];
    }
    public static short byteSum(short[] bytes){
    	int length=bytes.length;
    	short c1=0;        
    	for(int i=2;i<length;i++){
    		c1+=bytes[i];
    		c1=(short) (c1&255);
    	}
    	return c1;
    }
	private static final long MINUTE = 60000;

	public static boolean isSameTime(Date time1, Date time2, int minuteGap) {
		return Math.abs(time1.getTime() - time2.getTime()) <= MINUTE
				* minuteGap;
	}

	// private byte[] tests=
	// {(byte)0xEB,(byte)0x90,(byte)0x80,0x00,0x00,0x02,(byte)0x82};
	// EB908000000282
	public static String convertIntPortsToBytePorts(int[] ports) {
		int sports = 0;
		for (int port : ports) {
			sports += convertPortToIndex(port);
		}
		String bytePorts = String.format("%04X", sports);
		// bytePorts=bytePorts.substring(bytePorts.length()-4);
		return bytePorts;
	}

	private static int convertPortToIndex(int port) {
		switch (port) {
		case 16:return 1;
		case 15:return 2;
		case 14:return 4;
		case 13:return 8;
		case 12:return 16;
		case 11:return 32;
		case 10:return 64;
		case 9: return 128;
		case 8:	return 256;
		case 7:	return 512;
		case 6:	return 1024;
		case 5:	return 2048;
		case 4:	return 4096;
		case 3:	return 8192;
		case 2:	return 16384;
		default:
			return 32768;
		}
	}

	public static int btoi(byte rb) {
		return 0x00FF & rb;
	}

	public static int getIntNumber(int offset, int intLen, byte[] dataTemp) {
		int IntNumber = 0;
		switch (intLen) {
		case 1:return (int) Long.parseLong(String.format("%02X", dataTemp[offset + 1]), 16);
		case 2:return (int) Long.parseLong(String.format("%02X%02X",dataTemp[offset + 1], dataTemp[offset + 2]), 16);
		case 3:return (int) Long.parseLong(String.format("%02X%02X%02X",dataTemp[offset + 1], dataTemp[offset + 2],dataTemp[offset + 3]), 16);
		case 4:return (int) Long.parseLong(String.format("%02X%02X%02X%02X",dataTemp[offset + 1], dataTemp[offset + 2],dataTemp[offset + 3], dataTemp[offset + 4]), 16);
		case 5:return (int) Long.parseLong(String.format("%02X%02X%02X%02X%02X",dataTemp[offset + 1], dataTemp[offset + 2],dataTemp[offset + 3], dataTemp[offset + 4],dataTemp[offset + 5]), 16);
		case 6:return (int) Long.parseLong(String.format("%02X%02X%02X%02X%02X%02X", dataTemp[offset + 1],	dataTemp[offset + 2], dataTemp[offset + 3],dataTemp[offset + 4], dataTemp[offset + 5],dataTemp[offset + 6]), 16);
		}
		return IntNumber;
	}

	public static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
				.byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
				.byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}

	public static byte[] floatToBytes(Double parameter) {
		byte[] flow = new byte[4];
		float value = parameter.floatValue();
		if (value == 0) {
			flow[0] = (byte) 0x00;
			flow[1] = (byte) 0x00;
			flow[2] = (byte) 0x00;
			flow[3] = (byte) 0x00;
			return flow;
		}
		String src = Integer.toHexString(Float.floatToIntBits(value));
		byte[] tmp = src.getBytes();
		for (int i = 0; i < 4; i++) {
			flow[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return flow;
	}

	public static byte[] intToByte2(int i) {
		byte[] bt = new byte[2];
		bt[0] = (byte) (0xff & i);
		bt[1] = (byte) ((0xff00 & i) >> 8);
		return bt;
	}

	/**
	 * from: copy source to : copy target beginSite: copy target's begin site
	 * length:copy memory length by byte
	 * */
	public static void copyBuffer(byte[] from, byte[] to, int beginSite,int length) {
		if (from == null || to == null) {
			return;
		}
		int copyLength = length;
		if (length > to.length - beginSite) {
			copyLength = to.length - beginSite;
		}

		if (from.length < length) {
			copyLength = from.length;
		}
		for (int i = 0; i < copyLength; i++) {
			to[beginSite + i] = from[i];
		}
		return;
	}

	public static void memset(byte[] buffer, byte value) {
		if (buffer == null) {
			return;
		}
		for (int i = 0; i < buffer.length; i++) {
			buffer[i] = value;
		}
	}

	public static byte[] concatAll(byte[] first, byte[]... rest) {
		int totalLength = first.length;
		for (byte[] array : rest) {
			totalLength += array.length;
		}
		byte[] result = Arrays.copyOf(first, totalLength);
		int offset = first.length;
		for (byte[] array : rest) {
			System.arraycopy(array, 0, result, offset, array.length);
			offset += array.length;
		}
		return result;
	}
    public static byte nor(byte nor,byte[] data,int start,int end){
    	for (int i=start;i<end;i++){
    		nor ^=data[i];
    	}
    	return nor;
    }
    public static String toDataSignal(String pdata){
		StringBuilder buf = new StringBuilder(Long.toBinaryString(Long.parseLong(pdata,16)));
        while (buf.length() < 32) {
            buf.insert(0, "0");
        }
        return buf.toString();

	}
	public static void main(String[] args) throws UnsupportedEncodingException {
		//EB95100041213C1FC3B2810700065316072616244132451DC80035416443  //
		System.out.println(String.format("%04X",buildCrc16("EB95100041213C1FC3B2810700065316072616244132451DC8003541D4")));
		//System.out.println(buildCrc16("EB95100041213C1FC3B2810700065316072616244132451DC8003541D4"));
		/*System.out.println(Long.parseLong("057F",16));
		System.out.println(Integer.parseInt("05",16)<<8);
		System.out.println(Integer.parseInt("7F",16));
		System.out.println(Integer.parseInt("05",16)<<8|Integer.parseInt("7F",16));
		System.out.println((Integer.parseInt("05",16)<<8)+Integer.parseInt("7F",16));
		System.out.println(String.format("%04X",buildCrc16("EAAE010259AE")));
		System.out.println(String.format("%02X",byteSum("EB900E00E903")));*/
		//int target=Integer.parseInt("0B8B",16);
		//System.out.println(target>>6);
		//System.out.println(target&0x003F);
		//System.out.println(String.format("%02X",ByteUtil.byteSum("EB900900F106")));24 50 4A 4C 4F 44 2C 4E 4F 52 42 4F 4F 54 5F 56 31 2E 36 31 5F 31 30 77 2A 30 43 0D 0A
		              ///24   43   4F    55    54    2C   30    2C     31    2C   30    2C    31    34    34    36    35    31    2C    31     2C    32   39    2C    33   32     2C   EB95100041 
        //byte[] datas={0x24,0x43,0x4F ,0x55 ,0x54 ,0x2C ,0x30 ,0x2C ,0x31 ,0x2C ,0x30 ,0x2C ,0x31 ,0x34 ,0x34 ,0x36 ,0x35 ,0x31 ,0x2C ,0x31 ,0x2C ,0x32 ,0x39 ,0x2C ,0x33 ,0x32 ,0x2C};
        //System.out.println(new String(datas,"utf-8"));
        //System.out.println(new String(datas,"GB2312"));
        
	}
}
